//
//  TourOrderRootFourVC.m
//  EasyToTesco2.0
//
//  Created by knew on 2017/5/8.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "TourOrderRootFourVC.h"
#import "TourOrderModel.h"
#import "GoodsOrderInfoCell.h"

@interface TourOrderRootFourVC ()

<UITableViewDelegate,UITableViewDataSource>
{
    MBProgressHUD *progressHUD;
    UIImageView *noDrawImage;
    NSUInteger refreshIndex;
    NSDictionary *_picprex;
}
@property(nonatomic,strong)UITableView *goodsView;

/**
 订单
 */
@property(nonatomic,strong)NSMutableArray *goodsOrderArray;

@end

static NSString * const goodsOrderReuseIdentifier =  @"OrderZeroCell";

@implementation TourOrderRootFourVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    refreshIndex = 1;
    
    [self buildView];
    [self goodsOrderListRequestData:refreshIndex];
}

- (void)buildView
{
    
    self.goodsView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,WIDTH,HEIGHT-116 ) style:UITableViewStyleGrouped];
    [self.view addSubview:_goodsView];
    _goodsView.delegate = self;
    _goodsView.dataSource = self;
    _goodsView.rowHeight = 80;
    _goodsView.backgroundColor = BACKGROUND_COLOR;
    
    
    [self goodsOrderDataRefreshing];
}

//上啦更多
- (void)goodsOrderListRequestMoreData
{
    refreshIndex ++;
    [self goodsOrderListRequestData:refreshIndex];
    [_goodsView.mj_footer endRefreshing];
}

- (void)goodsOrderListRequesetDownData
{
    refreshIndex =1;
    [self goodsOrderListRequestData:refreshIndex];
    [_goodsView.mj_header endRefreshing];
}

#pragma mark >>>>>>>>刷新
- (void)goodsOrderDataRefreshing
{
    //上啦刷新
    _goodsView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(goodsOrderListRequestMoreData)];
    
    //下拉刷新
    MJChiBaoZiHeader *header = [MJChiBaoZiHeader headerWithRefreshingTarget:self refreshingAction:@selector(goodsOrderListRequesetDownData)];
    
    // 隐藏时间 和状态
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    //
    //    [header beginRefreshing];
    //    _goodsView.mj_header = header;
    
}

- (void)tourOrderPayDidClick:(UIButton *)sender
{
    TourOrderModel *goodsModel = self.goodsOrderArray[sender.tag];
    //limit_buy
    
    NSDictionary *userDict = @{@"orderId":goodsModel.order_no,@"zongjia":goodsModel.pay_amount};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_TOUR_ORDER_PAY object:nil userInfo:userDict];
}

#pragma mark >>>>>>>>>获取商品列表
- (void)goodsOrderListRequestData:(NSInteger)index
{
    
    //菊花加载
    progressHUD = [MBProgressHUD loadingInformationToView];
    [self.view addSubview:progressHUD];
    
    //避免下拉刷新重复数据
    if (refreshIndex ==1) {
        [self.goodsOrderArray removeAllObjects];
    }
    
    NSDictionary *dict = @{@"username":USERNAME,@"page":[NSString stringWithFormat:@"%zd",index],@"order_state":@"4"};
    //    NSLog(@"%@",dict);
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"http://test.suntao-develop.com/api/travel/order_list" completeBlock:^(id object) {
        
        NSLog(@"---------------%@",object);
        
        NSArray *tempArray = [object objectForKey:@"data"];
        
        if (tempArray.count) {
            for (NSDictionary *dic in tempArray) {
                TourOrderModel *model = [[TourOrderModel alloc] initWithDictionary:dic error:nil];
                [self.goodsOrderArray addObject:model];
            }
        } else {
            //订单没有
            if (refreshIndex == 1) {
                noDrawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"noOrder"]];
                [self.view addSubview:noDrawImage];
                
                [noDrawImage mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(self.view.mas_centerX);
                    make.centerY.equalTo(self.view.mas_centerY).offset(-HEIGHT/8);
                    make.width.mas_equalTo(WIDTH/2);
                    make.height.mas_equalTo(HEIGHT/4);
                }];
            }else{
                
                //没有更多数据
                [_goodsView.mj_footer endRefreshingWithNoMoreData];
                
            }
        }
        
        [progressHUD hide:YES];
        [_goodsView reloadData];
        progressHUD.removeFromSuperViewOnHide = true;
        
    }];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TourOrderModel *goodsModel = self.goodsOrderArray[indexPath.section];
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_TOUR_ORDER_DETAILS object:nil userInfo:@{@"model":goodsModel}];
}


#pragma mark >>>>>>>>>>数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.goodsOrderArray.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GoodsOrderInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:goodsOrderReuseIdentifier ];
    
    if (cell == nil) {
        cell = [[GoodsOrderInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:goodsOrderReuseIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
    TourOrderModel *goodsOrderModel = self.goodsOrderArray[indexPath.section];
    
    //        NSLog(@"goodsListsModel %@",goodsListsModel);
    
    
#warning 修改图片
    //图片
    [cell.goodsImg sd_setImageWithURL:[NSURL URLWithString:goodsOrderModel.picurl] placeholderImage:[UIImage imageNamed:@"goodsLoadJGG"]];
    
    
    cell.goodsName.text = goodsOrderModel.title;
    cell.goodsPrice.text = [NSString stringWithFormat:@"¥%@",goodsOrderModel.pay_amount];
    cell.goodsNum.text = [NSString stringWithFormat:@"x%@",goodsOrderModel.ticket_count];
    
    cell.guige.text = [NSString stringWithFormat:@"使用时间:%@\n取票密码:%@",goodsOrderModel.travel_date.length ? goodsOrderModel.travel_date : @"暂无使用时间信息",goodsOrderModel.password.length ? goodsOrderModel.password : @"暂无取票密码信息"];
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    TourOrderModel *goodsOrderModel = self.goodsOrderArray[section];
    
    UIView *headView = [[UIView alloc] init];
    headView.backgroundColor = [UIColor whiteColor];
    
    //订单状态
    UILabel *stutasLabel = [[UILabel alloc] init];
    stutasLabel.font = FONT(13);
    [headView addSubview:stutasLabel];
    stutasLabel.textColor = DEFAULT_COLOR;
    if ([goodsOrderModel.pay_mark isEqualToString:@"0"]) {
        stutasLabel.text = @"未付款";
    }else {
        stutasLabel.text = @"已付款";
    }
    
    //时间
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.font = FONT(13);
    [headView addSubview:timeLabel];
    timeLabel.textColor = [UIColor grayColor];
    timeLabel.text = goodsOrderModel.create_time;
    
    
    [stutasLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(headView.mas_centerY);
        make.left.equalTo(headView.mas_left).offset(10);
    }];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headView.mas_centerY);
        make.right.equalTo(headView.mas_right).offset(-10);
    }];
    
    return headView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    TourOrderModel *goodsOrderModel = self.goodsOrderArray[section];
    
    UIView *headView = [[UIView alloc] init];
    headView.backgroundColor = [UIColor whiteColor];
    
    
    UILabel *totalLbl = [[UILabel alloc] init];
    totalLbl.font = FONT(15);
    totalLbl.font = [UIFont boldSystemFontOfSize:15];
    [headView addSubview:totalLbl];
    totalLbl.textColor = DEFAULT_COLOR;
    totalLbl.text = [NSString stringWithFormat:@"总价:¥%@",goodsOrderModel.pay_amount];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,40,WIDTH,1)];
    lineView.backgroundColor = [UIColor blackColor];
    lineView.alpha = 0.1;
    [headView addSubview:lineView];
    
    //立即付款
    if ([goodsOrderModel.pay_mark isEqualToString:@"0"]) {
        
        UIButton *payBtn = [[UIButton alloc] init];
        [headView addSubview:payBtn];
        payBtn.tag = section;
        [payBtn setTitle:@"付款" forState:UIControlStateNormal];
        [payBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        payBtn.titleLabel.font = FONT(12);
        payBtn.layer.cornerRadius = 3;
        payBtn.layer.masksToBounds = YES;
        payBtn.layer.borderWidth = 0.5;
        payBtn.backgroundColor = DEFAULT_COLOR;
        payBtn.layer.borderColor = [UIColor whiteColor].CGColor;
        [payBtn addTarget:self action:@selector(tourOrderPayDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(25);
            make.centerY.equalTo(headView.mas_centerY).offset(15);
            make.right.equalTo(headView.mas_right).offset(-10);
        }];
    }
    
    UIView *separateView = [[UIView alloc] init];
    separateView.backgroundColor = BACKGROUND_COLOR;
    [headView addSubview:separateView];
    
    [separateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(WIDTH);
        make.height.mas_equalTo(10);
        make.bottom.equalTo(headView.mas_bottom);
    }];
    
    
    [totalLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headView.mas_top).offset(5);
        make.right.equalTo(headView.mas_right).offset(-10);
        make.height.mas_equalTo(35);
    }];
    
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    TourOrderModel *goodsOrderModel = self.goodsOrderArray[section];
    
    if ([goodsOrderModel.pay_mark isEqualToString:@"0"]) {
        return 90;
    } else {
        return 45;
    }
}

- (NSMutableArray *)goodsOrderArray {
    if (!_goodsOrderArray) {
        _goodsOrderArray = [NSMutableArray array];
    }
    return _goodsOrderArray;
}

@end
